// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.core.launch;

import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.IProcessFactory;
import org.eclipse.debug.core.model.IProcess;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class DenguesProcessFactory implements IProcessFactory {

    private static final String PROCESS_FACTORY_ID = "org.dengues.designer.core.launch.denguesProcessFactory"; //$NON-NLS-1$

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.debug.core.IProcessFactory#newProcess(org.eclipse.debug.core.ILaunch, java.lang.Process,
     * java.lang.String, java.util.Map)
     */
    public IProcess newProcess(ILaunch launch, Process process, String label, Map attributes) {
        return new DenguesRuntimeProcess(launch, process, label, attributes);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "launch".
     * 
     * @param proj
     * @param name
     * @param mainClass
     * @param args
     * @return
     * @throws CoreException
     */
    public static ILaunch launch(IJavaProject proj, String name, String mainClass, String args) throws CoreException {
        ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
        ILaunchConfigurationType type = manager.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
        ILaunchConfiguration config = null;
        // if the configuration already exists, use it!
        ILaunchConfiguration[] configurations = manager.getLaunchConfigurations(type);
        for (int i = 0; i < configurations.length; i++) {
            if (configurations[i].getName().equals(name))
                config = configurations[i];
        }
        // else create a new one
        if (config == null) {
            ILaunchConfigurationWorkingCopy wc = type.newInstance(null, name);
            wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, proj.getProject().getName());
            // current directory should be the project root
            wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, proj.getProject().getLocation().toString());
            // use the suplied args
            wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, mainClass);
            wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, args);
            wc.setAttribute(DebugPlugin.ATTR_PROCESS_FACTORY_ID, PROCESS_FACTORY_ID);
            // saves the new config
            config = wc.doSave();
        }
        return config.launch(ILaunchManager.RUN_MODE, null);
    }
}
